//
//  window.cpp
//  RPG
//
//  Created by Matt Bailey on 28/12/2013.
//  Copyright (c) 2013 Matt Bailey. All rights reserved.
//

#include "window.hpp"

GLFWwindow* Window::createWindow() {
    
    parseConfig();
    GLFWwindow* window;
    
    system("clear");
    
    cout << "Initialising..." << "\n";
    
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
    
    if (!window) {
        cout << "ERROR: Failed to Open Window" << endl;
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    
    int wTemp, hTemp;
    glfwGetWindowSize(window, &wTemp, &hTemp);
    
    cout << "OpenGL Version: " << glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR) << "." << glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR) << "\n";
    cout << "Window Size: " << wTemp << " x " << hTemp << "\n\n";
    
    width = wTemp;
    height = hTemp;
    
    return window;
}

void Window::scrollCallback (GLFWwindow*, double x, double y) {
    
    Camera::setZoom(y);
}

void Window::parseConfig() {
    
    stringstream ss;
    string line;
    
    ss.str("");
    ss << settings;

    while (getline(ss, line)) {
        
        stringstream ls(line);
        string variableName; int value;
        
        ls >> variableName >> value;
        
        //cout << value << endl;
        
        if (variableName == "width") {
            width = value;
        }
        
        if (variableName == "height") {
            height = value;
        }
        
        if (variableName == "vsync") {
            vsync = value;
        }
        
        if (variableName == "openGL_major") {
            major = value;
        }
        
        if (variableName == "openGL_minor") {
            minor = value;
        }
    }
}

int Window::width;
int Window::height;
int Window::vsync;
int Window::major;
int Window::minor;

string Window::title = "Miku RPG";
string Window::settings;
